package com.mach.platform.controller;

import com.mach.platform.domain.BaseCode;
import com.mach.platform.domain.Response;
import com.mach.platform.service.BaseCodeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.controller
 * @time 2018/8/13 0013 15:27
 */
@RequestMapping("dict")
@RestController
public class BaseCodeCtrl extends BaseCtrl<BaseCode> {

	@Autowired
	private BaseCodeServiceImpl baseCodeService;

	/**
	 * -- @GetMapping(value = "/info/{id}")
	 * -- @ApiOperation(value = "单个实体信息", notes = "接口备注：无", produces = "application/json")
	 * 单个数据 的接口
	 * -- @PathVariable(value = "id", required = true) Long id
	 *
	 * @param id 数据的id
	 * @return Response
	 */
	@Override
	public Response info (Long id) {
		return null;
	}

	/**
	 * --	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	 * -- @GetMapping(value = "/list")
	 * 获取列表数据
	 * -- @PageableDefault(size = 25) Pageable pageable
	 * -- @RequestParam Map params
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Response
	 */
	@GetMapping("list")
	@Override
	public Response list (@PageableDefault(size = 20, sort = {"sort"}) Pageable pageable, @RequestParam Map params) {

		Page<BaseCode> codePage = baseCodeService.list(pageable, params);

		return Response.ok(codePage);
	}

	/**
	 * -- @DeleteMapping("/del")
	 * -- @ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
	 * 删除数据
	 * -- @RequestBody Long[] ids
	 *
	 * @param ids id的集合
	 * @return Response
	 */
	@PreAuthorize("hasAuthority('ROLE_SUPER_ADMIN')")
	@DeleteMapping("del")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		int deleteNums = baseCodeService.delete(ids);

		return Response.ok(deleteNums);
	}

	/**
	 * --@PostMapping("/save")
	 * --	@ApiOperation(value = "保存", notes = "接口备注：无", produces = "application/json")
	 * 保存一个 对象
	 * --@RequestBody T t
	 *
	 * @param baseCode 保存的实体
	 * @return Response
	 */
	@PostMapping("save")
	@Override
	public Response save (@RequestBody BaseCode baseCode) {

		BaseCode baseCode1 = baseCodeService.save(baseCode);

		return Response.ok(baseCode1);
	}

	/**
	 * --@PutMapping("/upd")
	 * --	@ApiOperation(value = "更新", notes = "接口备注：无", produces = "application/json")
	 * 更新一个对象
	 * --@RequestBody T t
	 *
	 * @param baseCode 更新的实体
	 * @return Response
	 */
	@PutMapping("upd")
	@Override
	public Response update (@RequestBody BaseCode baseCode) {
		BaseCode baseCode1 = baseCodeService.update(baseCode);
		return Response.ok(baseCode1);
	}

	/**
	 * 获取选项的菜单
	 *
	 * @return
	 */
	@GetMapping("opts")
	public Response getOptions ( ) {
		Map optResults = baseCodeService.getOptions();
		return Response.ok(optResults);
	}

}
